<?php
	FLEA::loadClass('FLEA_Db_TableDataGateway');

	class	Model_Inventor	extends		FLEA_Db_TableDataGateway
	{
		var		$tableName		=	'inventor';
		var		$primaryKey		=	'InventorID';
		
		
		function	findAllInventors($clientID)
		{
			$sql	=	
				"
				select inventor.* , user.FirstName, user.LastName , user.Email,
				user.Address,user.City,user.State,user.Country,user.PostalCode,
				user.WorkPhone,user.HomePhone,user.CellPhone from user,inventor
				where inventor.UserID=user.UserID and 
				inventor.ClientID=".$clientID;
			$inventors	=	$this->findBySql($sql);
			return $inventors;
		}
		
		function	deleteInventor($inventorID)
		{
			$dbo	=	FLEA::getDBO();
			
			$sql	=	"delete from user where UserID in (select UserID from inventor where InventorID=".$inventorID.")";
			$dbo->execute($sql);
			
			$sql	=	"delete from inventor where InventorID=".$inventorID;
			$dbo->execute($sql);
			$sql	=	"update invention set InventorID=0 where InventorID=".$inventorID;
			$dbo->execute($sql);
			
			
			return 1;
		}
		
		function	findInventor($practitionerID)
		{
			$sql	=	
				"
				select practitioner.* , user.FirstName, user.LastName , user.Email,
				user.Address,user.City,user.State,user.Country,user.PostalCode from user,practitioner
				where practitioner.UserID=user.UserID and 
				practitioner.PractitionerID=".$practitionerID;
			$practitioners	=	$this->findBySql($sql);
			return $practitioners[0];
		}
		function	addInventor($firtName,$lastName,$email,$demo="",$clientID)
		{
			$_modelUser				=&	FLEA::getSingleton('Model_User');
			$user					=	$_modelUser->findByField("Email",$email);
	
			if($user["Email"]	==	$email)
			{
				/**
				 * The email is used,try other
				 */
				return -1;
			}
			else
			{
				$user["Email"]		=	$email;
				$user["Password"]	=	$email;
				$user['FirstName']	=	$firtName;
				$user['LastName']	=	$lastName;
	
				$userID				=	$_modelUser->create($user);
	
				$inventor['UserID']	=	$userID;
				$inventor['ClientID']=	$clientID;
	
				$inventorID			=	$this->create($inventor);
	
				if($inventorID)
				{
					$_modelClient	=&	FLEA::getSingleton('Model_Client');
					$client	=	$_modelClient->findClient($clientID);
					require("Extra/Mail.php");
					set_time_limit(100);
		
					$SmtpEmailTo 	= $email;
		
					$mailsubject 	= 	"[IP Manager] ------";
					$mailHeader		=	"Content-Type:text/HTML ";
					
					if($demo == "")
					{
						$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
						font-weight:800;'>
						You are registered to access the IP Manager site for ".$client['Business'].".<br><br>
						This email address is your userid.  Login here using your email address as the password.
						</div>";
					}
					else
					{
						$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
						font-weight:800;'>".$demo."</div>";
					}
		
					$smtp = new smtp(SmtpServer,Smtpserverport,true,SmtpUser,SmtpPass);
					$smtp->debug = true;
		
		
					$re				=
					$smtp->sendmail($SmtpEmailTo,SmtpUserMail,$mailsubject,$mailBody,"","","",$mailHeader);
					
					if($re)
					{
						return 1;
					}
					else
					{
						return 2;
					}
				}
			}
			return 0;
		}
	}
?>